<?php
//回收站功能
class themeControl extends Control{

	function theme_show(){
	    C("debug",1);
		$db=K("newsview");//使用扩展模型newsview;
		$catid=isset($_GET['catid'])?$_GET['catid']:false;//是否是通过点击栏目表进入的如果是就获取CID不然就返回false
		$where_catid=$catid?"catid='$catid'":'';//判断是否要按条件读取栏目表
		$where_status=isset($_GET['status'])?$_GET['status']:'';//是否显示未审核的文件;没太看懂
		$count=$db->where("$where_catid")->where("$where_status")->where("recycled=0")->count();//不在回收站的
		$page=new page($count,5);//计算分页的总数和每页显示数量
		$news=$db->field("id,title,click,cname,user.username,addtime,status")
				->where("$where_catid")->where("$where_status")->where("recycled=0")->limit($page->limit())->findall();
		$this->assign("news",$news);
		$this->assign("page",$page->show(2));
	    $this->display();
	}
	function add_show(){
	if(!isset($_GET['catid'])){
		$db=M("category");
		$category=$db->field("cname,catid,pid")->findall();
		$category=arr::channel($category,'catid','pid',0,2);//递归创建栏目;
		$this->assign("category",$category);//分配到页面
	}
	$this->display();
	}
	function add(){
	     $db=K("newsrelation");
	     $img=M("upload");
		 $_POST['username']=$_SESSION['username'];
		 $_POST['news_data']['userip']=ip_get_client();//插入到表NEWS_DATA中
		 $_POST['addtime']=date("y-m-d h:i:s",time());
		 $_POST['updatetime']=date("y-m-d h:i:s",time());
		 $_POST['catid']=!empty($_POST['catid'])?$_POST['catid']:99;
		 $description=isset($_POST['$description'])?$_POST['$description']:'';

		 $title=$_POST['title'];
		@$num=$db->add();//会报错但是可以执行 问问老师
		 if(!empty($num)){
		 	$_POST['id']=$num['news'];
		 foreach ($_POST['thumb'] as  $v) {
		 	$_POST['thumb']=$v['0'];
		 	$_POST['description']=$description;
		 	$_POST['title']=$title;
		 	$img->add();
		 }	
		 if($img->get_affected_rows()){
		 	$this->success("上传成功");
		 }else{
		 	$this->error("上传失败");
		 }
		 }else{
		 	$this->error("上传失败");
		 }
		 exit;
		 //thumb 缩略图  因为可能要上传多张所有用了2维数组
		 // if($db->add()){
		 // $this->success("文章添加成功 <a href='add_show'>继续添加</a><a href='theme_show'>查看文章列表</a>");
		 // }else{	
		 // $this->error("添加失败 <a href='addshow'>重新添加</a><a href='__APP__'>回到首页</a>"); 
		 // }
	} 
	function con_show(){
		C("debug",1);
		$id=$_GET['id'];
		$db=K("newsinfo");
		$upload=M("upload");
		$row=$db->field("user.username,news.id,title,body,addtime")->where("id='$id'")->findall();
		$thumb=$upload->field("thumb,id")->where("id='$id'")->findall();
		$this->assign("thumb",$thumb);
		$this->assign("row",$row);
	    $this->display();
	}
	function edit_show(){
		$id=$_GET['id'];
		$db=K("newsinfo");//等下修改获取栏目的名称
		$row=$db->field("id,title,body,keywords,description,catid")->where("id='$id'")->find();
		$this->assign("row",$row);
		$this->display();
	}
	function update(){//编辑文章
		$id=$_POST['id'];
		$db=K("newsrelation");
		$_POST['updatetime']=date("y-m-d h:i:s",time());
		$db=$db->update();
		if($db){
			$this->success("修改成功");	
		}else{
			$this->error("修改失败");
		}
	}
	function del(){//放入回收站
			$id=$_GET['id'];
			$db=M("news");
			if($db->save(array("id"=>$id,"recycled"=>1))){
				$this->success("已放入回收站");
			}else{
			$this->error("很抱歉操作失败");
			}
	}
	function delete(){//彻底删除

		$id=$_POST['data'];
		$db=K("newsrelation");
		if($db->where("id='$id'")->del()){
		echo 1;
		}else{
			echo 2;
		}
	}
	function recycled(){//回收站显示
			$db=K("newsview");
			$count = $db->where("recycled=1")->count();
		    $page = new page($count, 10);
			$news = $db->join("category")->field("id,title,click,status,news.catid,cname,addtime")->
		        where("recycled=1")->limit($page->limit())->all();
			$this->assign("news", $news);
		    $this->assign("page", $page->show(2));
		    $this->display();
	}
	function restore(){//还原
			$db=M("news");
			$id=$_GET['id'];
			if($db->save(array("id"=>$id,"recycled"=>0))){
				$this->success("已经还原");
			}else{
				$this->error("操作失败");
			}
		
	
	}



}



?>